package in.muton.edushare.pages.user;

import in.muton.edushare.url.URLLoader;

import java.util.ArrayList;

import org.apache.wicket.PageParameters;
import org.apache.wicket.RestartResponseException;
import org.apache.wicket.behavior.AttributeAppender;
import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.EmptyPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;

import com.edushare.common.constants.ApplicationConstants;
import com.edushare.common.constants.URLConstants;
import com.edushare.common.vo.ProfessionalDetails;

public class EditProfilePage extends UserBase {
	
	private Panel currentPanel;
	private Panel genPanel;
	private Panel expPanel;
	private Panel eduPanel;
	private Panel pubPanel;
	private ArrayList<WebMarkupContainer> highlightMe = new ArrayList<WebMarkupContainer>();
	
	public EditProfilePage(PageParameters params) {
		super(params);
		if(getModel().getObject().getUserId()!=getLoggedInUser().getUserId()){
			throw new RestartResponseException(URLLoader.getPageClass(URLConstants.UNAUTHORIZED_PATH));
		}
		
		this.genPanel = new EditProfileGeneralPanel("editPanel", super.getModel(), EditProfilePage.this);
		this.expPanel = new EditProfileExpPanel("editPanel", super.getModel(), EditProfilePage.this, ProfessionalDetails.PRO_DETAIL_TYPE_POSITION);
		this.eduPanel = new EditProfileExpPanel("editPanel", super.getModel(), EditProfilePage.this, ProfessionalDetails.PRO_DETAIL_TYPE_DEGREE);
		this.pubPanel = new EditPublicationsPanel("editPanel", super.getModel(), EditProfilePage.this);
		this.currentPanel = genPanel;
		add(currentPanel);
		loadPanel();
	}

	private void loadPanel() {
		
		final WebMarkupContainer genTab = new WebMarkupContainer("genTab");
		final WebMarkupContainer expTab = new WebMarkupContainer("expTab");
		final WebMarkupContainer eduTab = new WebMarkupContainer("eduTab");
		final WebMarkupContainer pubTab = new WebMarkupContainer("pubTab");
		add(genTab);
		add(expTab);
		add(eduTab);
		add(pubTab);
		
		highlightMe.add(genTab);
		highlightMe.add(expTab);
		highlightMe.add(eduTab);
		highlightMe.add(pubTab);
		//default
		highlightTab(0);
		
		genTab.add(new Link<Void>("gen"){

			private static final long serialVersionUID = -260403286741826447L;

			@Override
			public void onClick() {
				currentPanel.replaceWith(genPanel);
				currentPanel = genPanel;
				highlightTab(0);
			}

			
			
		});
		expTab.add(new Link<Void>("exp"){

			private static final long serialVersionUID = 8457982759094684908L;

			@Override
			public void onClick() {
				currentPanel.replaceWith(expPanel);
				currentPanel = expPanel;
				highlightTab(1);
			}
			
		});
		
		eduTab.add(new Link<Void>("edu"){

			private static final long serialVersionUID = 2637483085382573704L;

			@Override
			public void onClick() {
				currentPanel.replaceWith(eduPanel);
				currentPanel = eduPanel;
				highlightTab(2);
			}
			
		});
		
		pubTab.add(new Link<Void>("pub"){

			private static final long serialVersionUID = 1741418949438051885L;

			@Override
			public void onClick() {
				currentPanel.replaceWith(pubPanel);
				currentPanel = pubPanel;
				highlightTab(3);
			}
			
		});
	}
	
	public void highlightTab(int index) {
		int i = 0;
		for(WebMarkupContainer wmc: highlightMe){
			if(index==i){
				wmc.add(new SimpleAttributeModifier("class", "es-fbTabItemSelected"));
			}else{
				wmc.add(new SimpleAttributeModifier("class", "es-fbTabItem"));
			}
			i++;
		}
	}

	@Override
	public String getActivePage() {
		return "UserProfile";
	}

	@Override
	public String getPageTitle() {
		return "Edit My profile | " + ApplicationConstants.APPLICATION_NAME;
	}

	@Override
	public String getUserRole() {
		return "";
	}

}
